﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Windows.Forms.DataVisualization.Charting;

namespace HabitAnalyzer
{
    /// <summary>
    /// Interaction logic for CategoryReport.xaml
    /// </summary>
    public partial class ComparisonBreakoutReport : Window
    {

        DbUtility db = new DbUtility();
        public struct Totaler
        {
            public Category _cat;
            public TimeSpan _span;
            public string _catName;

            public TimeSpan span
            { get { return _span; } }

            public string catName
            { get { return _catName; } }
        }

        public struct PassedData
        {
            public DateTime start;
            public DateTime end;
            public int sessionid;
            public string title;
            public List<Category> cats;
        }

        PassedData init1;
        PassedData init2;

        ObservableCollection<Totaler> _ActInfo1 = new ObservableCollection<Totaler>();
        ObservableCollection<Totaler> _ActInfo2 = new ObservableCollection<Totaler>();
        ObservableCollection<Totaler> _Difference = new ObservableCollection<Totaler>();

        public ComparisonBreakoutReport(DateTime start1, DateTime end1, DateTime start2, DateTime end2, int sessionid, string title, List<Category> cats)
        {
            Console.WriteLine("c0");
            InitializeComponent();
            Console.WriteLine("c1");
            init1 = new PassedData();
            init1.start = start1;
            init1.end = end1;
            init1.sessionid = sessionid;
            init1.title = title;
            init1.cats = cats;
            this.Title = title;
            int i, j, count = cats.Count, acount;
            bool emptyflag = true;
            for (i = 0; i < count; i++)
            {
                List<SessionActivity> acts = db.GetActivitiesBetweenDatesByCategory(sessionid, start1, end1, cats.ElementAt(i).CategoryID);
                acount = acts.Count;
                TimeSpan total = TimeSpan.Zero;
                for (j = 0; j < acount; j++)
                {
                    total += acts.ElementAt(j).timeDifference;
                }
                Totaler tot = new Totaler();
                tot._cat = db.GetCategoryById(cats.ElementAt(i).CategoryID).Single();
                tot._span = total;
                tot._catName = tot._cat.Name;
                if (tot._span != TimeSpan.Zero)
                    emptyflag = false;
                _ActInfo1.Add(tot);
                Console.WriteLine(tot._catName);
            }
            Console.WriteLine("c2");


            //awful bubble sort
            /*for (i = 0; i < _ActInfo1.Count; i++)
                for (j = 0; j < _ActInfo1.Count - 1; j++)
                    if (_ActInfo1.ElementAt(j)._span < _ActInfo1.ElementAt(j + 1)._span)
                    {
                        Totaler temp = _ActInfo1.ElementAt(j);
                        _ActInfo1[j] = _ActInfo1[j + 1];
                        _ActInfo1[j + 1] = temp;
                    }
            */
            //System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost();
            Chart chart = new Chart();
            chart.ChartAreas.Add("Default");
            Console.WriteLine("c3");
            Series series = new Series();
            //series.Points.Add(5);
            //series.Points.Add(10);
            series.ChartType = SeriesChartType.Pie;
            for (i = 0; i < _ActInfo1.Count; i++)
                if (_ActInfo1[i]._span.Ticks != 0)
                    series.Points.AddXY(_ActInfo1[i]._catName, _ActInfo1[i]._span.Ticks);
            chart.Series.Add(series);
            //chart.Legends.Add("Default");
            chart.BackColor = System.Drawing.Color.FromArgb(249, 249, 249);
            chart.ChartAreas[0].BackColor = System.Drawing.Color.FromArgb(249, 249, 249);
            host1.Child = chart;
            Console.WriteLine("c4");


            init2 = new PassedData();
            init2.start = start2;
            init2.end = end2;
            init2.sessionid = sessionid;
            init2.title = title;
            init2.cats = cats;

            this.Title = title;
            Console.WriteLine("c5");
            for (i = 0; i < count; i++)
            {
                List<SessionActivity> acts = db.GetActivitiesBetweenDatesByCategory(sessionid, start2, end2, cats.ElementAt(i).CategoryID);
                acount = acts.Count;
                TimeSpan total = TimeSpan.Zero;
                for (j = 0; j < acount; j++)
                {
                    total += acts.ElementAt(j).timeDifference;
                }
                Totaler tot = new Totaler();
                tot._cat = db.GetCategoryById(cats.ElementAt(i).CategoryID).Single();
                tot._span = total;
                tot._catName = tot._cat.Name;
                if (tot._span != TimeSpan.Zero)
                    emptyflag = false;
                _ActInfo2.Add(tot);
                Console.WriteLine(tot._catName);
            }
            Console.WriteLine("c6");


            //awful bubble sort
            ObservableCollection<Totaler> temporary = new ObservableCollection<Totaler>(_ActInfo2);
            /*for (i = 0; i < _ActInfo2.Count; i++)
                for(j=0; j<_ActInfo2.Count; j++)
                {
                    if (_ActInfo1[i]._catName == _ActInfo2[j]._catName)
                        temporary[i] = _ActInfo2[j];
                    
                }
            _ActInfo2 = temporary;*/
            Console.WriteLine("c7");
            //System.Windows.Forms.Integration.WindowsFormsHost host2 = new System.Windows.Forms.Integration.WindowsFormsHost();
            Chart chart2 = new Chart();
            chart2.ChartAreas.Add("Default");
            Series series2 = new Series();
            //series.Points.Add(5);
            //series.Points.Add(10);
            series2.ChartType = SeriesChartType.Pie;
            for (i = 0; i < _ActInfo2.Count; i++)
                if (_ActInfo2[i]._span.Ticks != 0)
                    series2.Points.AddXY(_ActInfo2[i]._catName, _ActInfo2[i]._span.Ticks);
            chart2.Series.Add(series2);
            chart2.BackColor = System.Drawing.Color.FromArgb(249,249,249);
            chart2.ChartAreas[0].BackColor = System.Drawing.Color.FromArgb(249, 249, 249);
            //chart.Legends.Add("Default");
            host2.Child = chart2;
            Console.WriteLine(" I MADE IT TO THE END!!! ");
            Console.WriteLine(ActInfo1.Count);
            Console.WriteLine(ActInfo2.Count);
            for (i = 0; i < ActInfo1.Count; i++)
            {
                Totaler temp = new Totaler();
                temp._catName = ActInfo1[i]._catName;
                temp._span = ActInfo1[i]._span - ActInfo2[i]._span;
                _Difference.Add(temp);
            }
            if (emptyflag == true)
            {
                label1.Visibility = Visibility.Visible;
                label2.Visibility = Visibility.Hidden;
                label3.Visibility = Visibility.Hidden;
                label4.Visibility = Visibility.Hidden;
                label5.Visibility = Visibility.Hidden;
                table1.Visibility = Visibility.Hidden;
                table2.Visibility = Visibility.Hidden;
                CategoryDetail.Visibility = Visibility.Hidden;
                differenceTable.Visibility = Visibility.Hidden;
                host1.Visibility = Visibility.Hidden;
                host2.Visibility = Visibility.Hidden;
            }
        }

        public ObservableCollection<Totaler> ActInfo1
        { get { return _ActInfo1; } }

        public ObservableCollection<Totaler> ActInfo2
        { get { return _ActInfo2; } }

        public ObservableCollection<Totaler> Difference
        { get { return _Difference; } }

        private void CategoryDetail_Click(object sender, RoutedEventArgs e)
        {
            if (table1.SelectedIndex == -1)
            {
                System.Windows.Forms.MessageBox.Show("Please select a category from the table.");
                return;
            }
            List<SessionActivity> sa = db.GetActivitiesBetweenDatesByCategorySorted(init1.sessionid, init1.start, init1.end, _ActInfo1[table1.SelectedIndex]._cat.CategoryID);
            Console.WriteLine("TaDa!");
            Console.WriteLine(_ActInfo1[table1.SelectedIndex]._cat.Name);
            Window report = new BasicReport(sa, "Category Detail Report");
            report.Show();
        }

    }
}
